package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.FlavorMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private FlavorMapper flavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Override
    @Transactional
    public void save(DishDTO dishDTO) {
        Dish dish = new Dish();

        //把dishDTO转换成entity
        BeanUtils.copyProperties(dishDTO,dish);

        //插入菜品信息后主键返回
        dishMapper.saveDish(dish);

        //获取flavor，遍历插入主键
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();

        //插入数据库中
        if(flavors.size() > 0) {
            for(int i = 0;i<flavors.size();i++) {
                flavors.get(i).setDishId(id);
            }
            flavorMapper.saveFlavor(flavors);
        }

    }

    @Override
    public PageResult selectPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        List<DishVO> dishVOs = dishMapper.pageSelect(dishPageQueryDTO);
        Page<DishVO> dishVOs1 = (Page<DishVO>) dishVOs;
        return new PageResult(dishVOs1.getTotal(),dishVOs1.getResult());
    }

    @Override
    @Transactional
    public boolean deleteDish(List<Long> ids) {

        //判断当前菜品是不是起售中的菜品 status
        for(Long id : ids) {
            Dish dish = dishMapper.selectAllById(id);
            if(dish.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }

        //判断当前菜品是不是被套餐关联了
        for(Long id : ids) {
            Integer idCount =  setmealDishMapper.selectByDishIDCount(id);
            if(idCount != 0) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
            }
        }

        //删除菜品
        Integer i = dishMapper.DeleteDish(ids);


        //删除菜品关联的口味数据
        if(i > 0) {
            Integer count = flavorMapper.DeleteFlavor(ids);
            if(count>0) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根据id查询某菜品并回显
     * @param id
     * @return
     */
    @Override
    public DishVO selectById(String id) {

        Dish dish = dishMapper.selectById(id);

        if(dish != null) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(dish,dishVO);
            List<DishFlavor> dishFlavors = flavorMapper.selectByDishId(id);
            if(dishFlavors != null) {
                dishVO.setFlavors(new ArrayList<>(dishFlavors));
            }
            return dishVO;
        }

        return null;
    }

    @Override
    @Transactional
    public boolean updateDish(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO,dish);

        Integer i = dishMapper.updateDish(dish);

        ArrayList<DishFlavor> dishFlavors = new ArrayList<>(dishDTO.getFlavors());
        //把之前的记录全部删掉
        List<Long> longs = new ArrayList<>();
        longs.add(dishDTO.getId());
        flavorMapper.DeleteFlavor(longs);
        if(dishFlavors.size()!=0) {
            for(DishFlavor dishFlavor : dishFlavors) {
                dishFlavor.setDishId(dishDTO.getId());
            }
            //再把当前的每条都加上去
            flavorMapper.saveFlavor(dishFlavors);
        }

        if(i>0) {
            return true;
        }

        return false;
    }
}
